package com.mr.example;

import com.mr.core.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordCountMapper implements Mapper<String, String, String, Integer> {
    // 新增单词模式匹配正则表达式（支持连字符、省略号等语法）
    private static final Pattern WORD_PATTERN = Pattern.compile(
        "\\b[a-zA-Z](?:[a-zA-Z']{1,28}[a-zA-Z])?\\b|\\b\\w+[-']\\w+\\b"
    );
    
    @Override
    public void map(String key, String value, Context<String, Integer> context) {
        // 文本处理逻辑
        String processedValue = value.toLowerCase()
            .replaceAll("[^a-zA-Z0-9'\\-]", " "); // 保留连字符和撇号
        
        Matcher matcher = WORD_PATTERN.matcher(processedValue);
        while (matcher.find()) {
            String word = matcher.group();
            if (!word.isEmpty()) {
                context.write(word, 1);
            }
        }
    }
}